#encoding=utf-8
from django.contrib.auth.models import User as AuthUser
from myhomework.models import *
from myhomework.exceptions.exception import *
from myhomework.utils import get_param
from django.http import HttpResponse
from django.utils.decorators import available_attrs
from myhomework.account.daos import *
from myhomework.homework.daos import *


class CommentDao(object):



    @staticmethod
    def create_comment(author_id, comment):
        
        author = UserDao.get_user_by_student_id(author_id)
        
        """
        homework = HomeworkDao.get_homework_by_id(homework_id)
        
        if ( not parent_comment_id is None):
            parent_comment = CommentDao.get_comment_by_id(parent_comment_id)
        else:
            parent_comment = None
        """
        
        comment.author = author
        
        """
        comment.homework = homework
        comment.parent_comment = parent_comment
        """
        parent_comment = comment.parent_comment
        if ( parent_comment is None ):
            comment.depth = 0
        else:
            comment.depth = parent_comment.depth + 1
        
        if ( comment.depth>=5 ):
            comment.depth = 4
        comment.content = comment.content.replace(r'&',r'&amp;')    
        comment.content = comment.content.replace(r"'",r"&acute;")
        comment.content = comment.content.replace(r'"',r'&quot;')
        comment.content = comment.content.replace(r'<',r'&lt;')
        comment.content = comment.content.replace(r'>',r'&gt;')
        comment.content = comment.content.replace('\r\n',r'<br/>')
        comment.content = comment.content.replace('\r',r'<br/>')
        comment.content = comment.content.replace('\n',r'<br/>')
        
        
        comment.save() 


    @staticmethod
    def delete_comment(comment_id):
        
        comment = CommentDao.get_comment_by_id(comment_id)
        
        comment.delete()
        
        pass
    
    
    @staticmethod
    def get_comment_by_id(comment_id):
        
        all_comment = Comment.objects.filter(id=comment_id)
        
        if (not all_comment):
            raise CommentNotExisted(comment_id)
        
        comment = all_comment.get()
        
        return comment
    @staticmethod
    def get_comments(homework_id):
        
        return Comment.objects.filter(homework=homework_id)

#comment = CommentDao.get_comment_by_id('18')
#print comment.author.name

"""
homework = HomeworkDao.get_homework_by_id('1')
parent_comment =  CommentDao.get_comment_by_id('18')
new_comment = Comment(homework=homework, parent_comment=parent_comment, content=" 'hello' 'world' ", pub_date='2010-1-1')

CommentDao.create_comment('001', new_comment)
"""

"""
new_comment = Comment(content='good bye son 2', pub_date='2010-1-1')
CommentDao.add_comment('001', '2', new_comment, '11')
"""

#CommentDao.delete_comment('14')
